package cn.sczc.jh.client.service.equipment;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.sczc.jh.client.entity.Equipment;
import cn.sczc.jh.client.entity.EquipmentPos;
import cn.sczc.jh.client.entity.Role;
import cn.sczc.jh.client.mapper.equipment.EquipmentPosMapper;
import cn.sczc.jh.client.vo.UserVo;

@Service
@Transactional(rollbackFor = Exception.class)
public class EquipmentPosService extends ServiceImpl<EquipmentPosMapper, EquipmentPos> {

	@Autowired
	private EquipmentPosMapper equipmentPosMapper;

	public int insertOrUpdate(EquipmentPos pos) {
		Map<String, Object> params = new HashMap<>();
		params.put("equNo", pos.getEquNo());
		params.put("equChannel", pos.getEquChannel());
		params.put("tenantId", pos.getTenantId());
		EquipmentPos ep = equipmentPosMapper.getEquipmentPos(params);
		if (ep != null) {
			pos.setOid(ep.getOid());
			pos.setUpdateTime(new Date());
			return equipmentPosMapper.updateById(pos);
		} else {
			pos.setOid(UUID.randomUUID().toString());
			pos.setCreateTime(new Date());
			return equipmentPosMapper.insert(pos);
		}
	}

	public int delPos(EquipmentPos pos) {
		QueryWrapper<EquipmentPos> wrapper = new QueryWrapper<>();
		wrapper.eq("equ_no", pos.getEquNo());
		wrapper.eq("equ_channel", pos.getEquChannel());
		wrapper.eq("tenant_id", pos.getTenantId());
		return equipmentPosMapper.delete(wrapper);
	}

	public EquipmentPos getEquipmentPos(Map<String, Object> params) {
		EquipmentPos equp = equipmentPosMapper.getEquipmentPos(params);
		return equp;
	}
	
	public Page<EquipmentPos> getListByParams(Page<EquipmentPos> page, Map<String, Object> params) {
		List<EquipmentPos> list = equipmentPosMapper.getListByParams(page, params);
		page.setRecords(list);
		return page;
	}

}
